package com.example.shushiyunsst.fragment.main.home.child.miner22

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Bitmap
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.example.library_common.base.LazyFragment
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.CMLIntent
import com.example.shushiyunsst.util.EncodingHandler

/**
 * CML收款
 * */
class CMLCollectionFragment : LazyFragment() {
    companion object {
        fun newInstance(mCMLIntent: CMLIntent): CMLCollectionFragment {
            val args = Bundle()
            args.putSerializable("mCMLIntent", mCMLIntent)
            val fragment = CMLCollectionFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var mCMLIntent: CMLIntent? = null

    private lateinit var mCopyTv: TextView

    private lateinit var addressTv: TextView

    private lateinit var erc20Btn: TextView

    private lateinit var sstBtn: TextView

    private lateinit var mCodingImg: ImageView

    private var createQRCode: Bitmap? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
        mCMLIntent = arguments?.getSerializable("mCMLIntent") as CMLIntent
    }

    override fun lazyInit() {
        sstBtn.setOnClickListener {
            sstBtn.isSelected = true
            erc20Btn.isSelected = false
        }
        erc20Btn.setOnClickListener {
            sstBtn.isSelected = false
            erc20Btn.isSelected = true
        }

        mCopyTv.setOnClickListener {

            val clipboardManager: ClipboardManager =
                _mActivity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val clipData = ClipData.newPlainText("playerId", addressTv.text)
            clipboardManager.setPrimaryClip(clipData)

            Toast.makeText(_mActivity, "复制成功", Toast.LENGTH_SHORT).show()
        }

        sstBtn.performClick()

        addressTv.text = mCMLIntent?.address

        if ((mCMLIntent?.address?:"").isNotEmpty()) {
            try {
                createQRCode = EncodingHandler.createQRCode((mCMLIntent?.address?:""), PixUtils.dp2px(200))
                mCodingImg.setImageBitmap(createQRCode)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    override fun initView(view: View) {
        loadRootFragment(R.id.wallet_header, CMLAllCaptureFragment.newInstance(mCMLIntent))

        mCopyTv = view.findViewById(R.id.mCopyTv)
        addressTv = view.findViewById(R.id.addressTv)
        erc20Btn = view.findViewById(R.id.erc20Btn)
        sstBtn = view.findViewById(R.id.sstBtn)
        mCodingImg = view.findViewById(R.id.mCodingImg)

        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)


        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
    }

    override fun getLayoutId(): Any {
        return R.layout.cml_collection_fragment
    }

    override fun onDestroy() {
        super.onDestroy()
        if (createQRCode?.isRecycled == false) {
            createQRCode?.recycle()
        }
    }
}